package handler

import (
	"github.com/unidoc/unioffice/document"
	"log"
	"org.knight/data"
	"strings"
)

type SimpleTableHandler struct {

}

func (sth SimpleTableHandler) handle(table document.Table)  {

	d := data.TableData{}
	for i, row := range table.Rows() {


		cells := row.Cells()
		l := len(cells)
		if i == 0 {
			continue
		}else if i == 1 {
			d.Header = make([]data.KVPair, l)

			for _, cell := range cells {
				d.Header = append(d.Header, data.KVPair{K: extractCellText(cell),V: int32(len(cell.Paragraphs()))})
			}
			continue
		}

		my_row := make([]data.KVPair, l)
		for j, cell := range cells {
			log.Printf("row %d cell %d, content=%s \n", i, j, extractCellText(cell))
			my_row = append(my_row, data.KVPair{K: extractCellText(cell),V: int32(len(cell.Paragraphs()))})
		}

		d.Rows = append(d.Rows, my_row)

	}

}


func extractCellText( cell document.Cell) string {

	var builder strings.Builder
	for _, p := range cell.Paragraphs() {
		for _, r := range p.Runs() {
			builder.WriteString(r.Text());
			builder.WriteString("\n")
		}
	}
	return  builder.String()
}